package com.spark.tags;

import com.spark.core.SparkContext;
import com.spark.core.XMap;

public class Form extends Box {
	private static final String DEFAULT_HEAD_CLASS = "form_head";
	private static final String DEFAULT_BODY_CLASS = "form_body";

	public static final String PROPERTITY_TITLE = "title";
	

	protected void buildHead(StringBuilder sb, SparkContext context, XMap view, int rows, int columns) throws Exception {
		String title = view.getString(PROPERTITY_TITLE);
		if (title != null) {
			sb.append("<thead><tr><th class='" + DEFAULT_HEAD_CLASS + "' colspan=" + columns * 2 + ">");
			sb.append(title);
			sb.append("</th></tr></thead>");
//			sb.append("<CAPTION class='").append(DEFAULT_HEAD_CLASS).append("'>");
//			sb.append(title);
//			sb.append("</CAPTION>");
			
		}
	}

	protected void afterBuildTop(StringBuilder sb, SparkContext context, XMap view) throws Exception {
		sb.append("<tbody class='" + DEFAULT_BODY_CLASS + "'>");

		boolean showmargin = view.getBoolean(PROPERTITY_SHOW_MARGIN,true);
		boolean showBorder = view.getBoolean(PROPRRTITY_SHOW_BORDER,false);
		if (showmargin && !showBorder)
			sb.append("<tr height='5'><td></td></tr>");
		super.afterBuildTop(sb, context, view);
	}

	protected void buildTop(StringBuilder sb, SparkContext context, XMap view,int rows, int columns,String id) throws Exception {

		String cls = view.getString(PROPERTITY_CLASSNAME, "");
		String style = view.getString(PROPERTITY_STYLE, "");
		int cellspacing = view.getInt(PROPERTITY_CELLSPACING, 0);
		int cellpadding = view.getInt(PROPERTITY_CELLPADDING, 0);

		int width = getComponentWidth(view, context);
		int height = getComponentHeight(view, context);
		boolean showBorder = view.getBoolean(PROPRRTITY_SHOW_BORDER,false);

		String className = DEFAULT_TABLE_CLASS + " layout-form";
		String title = view.getString(PROPERTITY_TITLE, "");
		if (title!=null) className += " " + TITLE_CLASS;
		className += " " + cls;
		if (showBorder) {
			cellspacing = 1;
			className += " layout-border";
			style += " border:none;";
		}

		sb.append("<table border=0 class='" + className + "' id='" + id + "'");
		if (width != 0) sb.append(" width=" + width);
		if (height != 0) sb.append(" height=" + height);
		if (!"".equals(style)) sb.append(" style='" + style + "'");
		sb.append(" cellpadding=" + cellpadding + " cellspacing=" + cellspacing + ">");
		buildHead(sb,context, view, rows, columns);
		afterBuildTop(sb,context, view);
	}

	protected void buildFoot(StringBuilder sb, SparkContext context, XMap view) throws Exception {
		super.afterBuildTop(sb, context, view);
		boolean showmargin = view.getBoolean(PROPERTITY_SHOW_MARGIN,true);
		boolean showBorder = view.getBoolean(PROPRRTITY_SHOW_BORDER,false);
		if (showmargin && !showBorder) sb.append("<tr height='5'><td></td></tr>");
	}

}
